using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
namespace ICSharpCode.TextEditor.Document
{
	public class DefaultHighlightingStrategy : IHighlightingStrategyUsingRuleSets, IHighlightingStrategy
	{
		private string name;
		private List<HighlightRuleSet> rules = new List<HighlightRuleSet>();
		private Dictionary<string, HighlightColor> environmentColors = new Dictionary<string, HighlightColor>();
		private Dictionary<string, string> properties = new Dictionary<string, string>();
		private string[] extensions;
		private HighlightColor digitColor;
		private HighlightRuleSet defaultRuleSet;
		private HighlightColor defaultTextColor;
		protected LineSegment currentLine;
		protected int currentLineNumber;
		protected SpanStack currentSpanStack;
		protected bool inSpan;
		protected Span activeSpan;
		protected HighlightRuleSet activeRuleSet;
		protected int currentOffset;
		protected int currentLength;
		public HighlightColor DigitColor
		{
			get
			{
				return this.digitColor;
			}
			set
			{
				this.digitColor = value;
			}
		}
		public IEnumerable<KeyValuePair<string, HighlightColor>> EnvironmentColors
		{
			get
			{
				return this.environmentColors;
			}
		}
		public Dictionary<string, string> Properties
		{
			get
			{
				return this.properties;
			}
		}
		public string Name
		{
			get
			{
				return this.name;
			}
		}
		public string[] Extensions
		{
			get
			{
				return this.extensions;
			}
			set
			{
				this.extensions = value;
			}
		}
		public List<HighlightRuleSet> Rules
		{
			get
			{
				return this.rules;
			}
		}
		public HighlightColor DefaultTextColor
		{
			get
			{
				return this.defaultTextColor;
			}
		}
		protected void ImportSettingsFrom(DefaultHighlightingStrategy source)
		{
			if (source == null)
			{
				throw new ArgumentNullException("source");
			}
			this.properties = source.properties;
			this.extensions = source.extensions;
			this.digitColor = source.digitColor;
			this.defaultRuleSet = source.defaultRuleSet;
			this.name = source.name;
			this.rules = source.rules;
			this.environmentColors = source.environmentColors;
			this.defaultTextColor = source.defaultTextColor;
		}
		public DefaultHighlightingStrategy() : this("Default")
		{
		}
		public DefaultHighlightingStrategy(string name)
		{
			this.name = name;
			this.digitColor = new HighlightColor(SystemColors.WindowText, false, false);
			this.defaultTextColor = new HighlightColor(SystemColors.WindowText, false, false);
			this.environmentColors["Default"] = new HighlightBackground("WindowText", "Window", false, false);
			this.environmentColors["Selection"] = new HighlightColor("HighlightText", "Highlight", false, false);
			this.environmentColors["VRuler"] = new HighlightColor("ControlLight", "Window", false, false);
			this.environmentColors["InvalidLines"] = new HighlightColor(Color.Red, false, false);
			this.environmentColors["CaretMarker"] = new HighlightColor(Color.Yellow, false, false);
			this.environmentColors["CaretLine"] = new HighlightBackground("ControlLight", "Window", false, false);
			this.environmentColors["LineNumbers"] = new HighlightBackground("ControlDark", "Window", false, false);
			this.environmentColors["FoldLine"] = new HighlightColor("ControlDark", false, false);
			this.environmentColors["FoldMarker"] = new HighlightColor("WindowText", "Window", false, false);
			this.environmentColors["SelectedFoldLine"] = new HighlightColor("WindowText", false, false);
			this.environmentColors["EOLMarkers"] = new HighlightColor("ControlLight", "Window", false, false);
			this.environmentColors["SpaceMarkers"] = new HighlightColor("ControlLight", "Window", false, false);
			this.environmentColors["TabMarkers"] = new HighlightColor("ControlLight", "Window", false, false);
		}
		public HighlightRuleSet FindHighlightRuleSet(string name)
		{
			foreach (HighlightRuleSet current in this.rules)
			{
				if (current.Name == name)
				{
					return current;
				}
			}
			return null;
		}
		public void AddRuleSet(HighlightRuleSet aRuleSet)
		{
			HighlightRuleSet highlightRuleSet = this.FindHighlightRuleSet(aRuleSet.Name);
			if (highlightRuleSet != null)
			{
				highlightRuleSet.MergeFrom(aRuleSet);
				return;
			}
			this.rules.Add(aRuleSet);
		}
		public void ResolveReferences()
		{
			this.ResolveRuleSetReferences();
			this.ResolveExternalReferences();
		}
		private void ResolveRuleSetReferences()
		{
			foreach (HighlightRuleSet current in this.Rules)
			{
				if (current.Name == null)
				{
					this.defaultRuleSet = current;
				}
				foreach (Span span in current.Spans)
				{
					if (span.Rule != null)
					{
						bool flag = false;
						foreach (HighlightRuleSet current2 in this.Rules)
						{
							if (current2.Name == span.Rule)
							{
								flag = true;
								span.RuleSet = current2;
								break;
							}
						}
						if (!flag)
						{
							span.RuleSet = null;
							throw new HighlightingDefinitionInvalidException("The RuleSet " + span.Rule + " could not be found in mode definition " + this.Name);
						}
					}
					else
					{
						span.RuleSet = null;
					}
				}
			}
			if (this.defaultRuleSet == null)
			{
				throw new HighlightingDefinitionInvalidException("No default RuleSet is defined for mode definition " + this.Name);
			}
		}
		private void ResolveExternalReferences()
		{
			foreach (HighlightRuleSet current in this.Rules)
			{
				current.Highlighter = this;
				if (current.Reference != null)
				{
					IHighlightingStrategy highlightingStrategy = HighlightingManager.Manager.FindHighlighter(current.Reference);
					if (highlightingStrategy == null)
					{
						throw new HighlightingDefinitionInvalidException(string.Concat(new string[]
						{
							"The mode defintion ", 
							current.Reference, 
							" which is refered from the ", 
							this.Name, 
							" mode definition could not be found"
						}));
					}
					if (!(highlightingStrategy is IHighlightingStrategyUsingRuleSets))
					{
						throw new HighlightingDefinitionInvalidException(string.Concat(new string[]
						{
							"The mode defintion ", 
							current.Reference, 
							" which is refered from the ", 
							this.Name, 
							" mode definition does not implement IHighlightingStrategyUsingRuleSets"
						}));
					}
					current.Highlighter = (IHighlightingStrategyUsingRuleSets)highlightingStrategy;
				}
			}
		}
		public void SetColorFor(string name, HighlightColor color)
		{
			if (name == "Default")
			{
				this.defaultTextColor = new HighlightColor(color.Color, color.Bold, color.Italic);
			}
			this.environmentColors[name] = color;
		}
		public HighlightColor GetColorFor(string name)
		{
			HighlightColor result;
			if (this.environmentColors.TryGetValue(name, out result))
			{
				return result;
			}
			return this.defaultTextColor;
		}
		public HighlightColor GetColor(IDocument document, LineSegment currentSegment, int currentOffset, int currentLength)
		{
			return this.GetColor(this.defaultRuleSet, document, currentSegment, currentOffset, currentLength);
		}
		protected virtual HighlightColor GetColor(HighlightRuleSet ruleSet, IDocument document, LineSegment currentSegment, int currentOffset, int currentLength)
		{
			if (ruleSet == null)
			{
				return null;
			}
			if (ruleSet.Reference != null)
			{
				return ruleSet.Highlighter.GetColor(document, currentSegment, currentOffset, currentLength);
			}
			return (HighlightColor)ruleSet.KeyWords[document, currentSegment, currentOffset, currentLength];
		}
		public HighlightRuleSet GetRuleSet(Span aSpan)
		{
			if (aSpan == null)
			{
				return this.defaultRuleSet;
			}
			if (aSpan.RuleSet == null)
			{
				return null;
			}
			if (aSpan.RuleSet.Reference != null)
			{
				return aSpan.RuleSet.Highlighter.GetRuleSet(null);
			}
			return aSpan.RuleSet;
		}
		public virtual void MarkTokens(IDocument document)
		{
			if (this.Rules.Count == 0)
			{
				return;
			}
			for (int i = 0; i < document.TotalNumberOfLines; i++)
			{
				LineSegment lineSegment = (i > 0) ? document.GetLineSegment(i - 1) : null;
				if (i >= document.LineSegmentCollection.Count)
				{
					break;
				}
				this.currentSpanStack = ((lineSegment != null && lineSegment.HighlightSpanStack != null) ? lineSegment.HighlightSpanStack.Clone() : null);
				if (this.currentSpanStack != null)
				{
					while (!this.currentSpanStack.IsEmpty && this.currentSpanStack.Peek().StopEOL)
					{
						this.currentSpanStack.Pop();
					}
					if (this.currentSpanStack.IsEmpty)
					{
						this.currentSpanStack = null;
					}
				}
				this.currentLine = document.LineSegmentCollection[i];
				if (this.currentLine.Length == -1)
				{
					return;
				}
				this.currentLineNumber = i;
				List<TextWord> words = this.ParseLine(document);
				if (this.currentLine.Words != null)
				{
					this.currentLine.Words.Clear();
				}
				this.currentLine.Words = words;
				this.currentLine.HighlightSpanStack = ((this.currentSpanStack == null || this.currentSpanStack.IsEmpty) ? null : this.currentSpanStack);
			}
			document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea));
			document.CommitUpdate();
			this.currentLine = null;
		}
		private bool MarkTokensInLine(IDocument document, int lineNumber, ref bool spanChanged)
		{
			this.currentLineNumber = lineNumber;
			bool result = false;
			LineSegment lineSegment = (lineNumber > 0) ? document.GetLineSegment(lineNumber - 1) : null;
			this.currentSpanStack = ((lineSegment != null && lineSegment.HighlightSpanStack != null) ? lineSegment.HighlightSpanStack.Clone() : null);
			if (this.currentSpanStack != null)
			{
				while (!this.currentSpanStack.IsEmpty && this.currentSpanStack.Peek().StopEOL)
				{
					this.currentSpanStack.Pop();
				}
				if (this.currentSpanStack.IsEmpty)
				{
					this.currentSpanStack = null;
				}
			}
			this.currentLine = document.LineSegmentCollection[lineNumber];
			if (this.currentLine.Length == -1)
			{
				return false;
			}
			List<TextWord> words = this.ParseLine(document);
			if (this.currentSpanStack != null && this.currentSpanStack.IsEmpty)
			{
				this.currentSpanStack = null;
			}
			if (this.currentLine.HighlightSpanStack != this.currentSpanStack)
			{
				if (this.currentLine.HighlightSpanStack == null)
				{
					result = false;
					using (SpanStack.Enumerator enumerator = this.currentSpanStack.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Span current = enumerator.Current;
							if (!current.StopEOL)
							{
								spanChanged = true;
								result = true;
								break;
							}
						}
						goto IL_229;
					}
				}
				if (this.currentSpanStack == null)
				{
					result = false;
					using (SpanStack.Enumerator enumerator2 = this.currentLine.HighlightSpanStack.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							Span current2 = enumerator2.Current;
							if (!current2.StopEOL)
							{
								spanChanged = true;
								result = true;
								break;
							}
						}
						goto IL_229;
					}
				}
				SpanStack.Enumerator enumerator3 = this.currentSpanStack.GetEnumerator();
				SpanStack.Enumerator enumerator4 = this.currentLine.HighlightSpanStack.GetEnumerator();
				bool flag = false;
				while (!flag)
				{
					bool flag2 = false;
					while (enumerator3.MoveNext())
					{
						if (!enumerator3.Current.StopEOL)
						{
							flag2 = true;
							break;
						}
					}
					bool flag3 = false;
					while (enumerator4.MoveNext())
					{
						if (!enumerator4.Current.StopEOL)
						{
							flag3 = true;
							break;
						}
					}
					if (flag2 || flag3)
					{
						if (flag2 && flag3)
						{
							if (enumerator3.Current != enumerator4.Current)
							{
								flag = true;
								result = true;
								spanChanged = true;
							}
						}
						else
						{
							spanChanged = true;
							flag = true;
							result = true;
						}
					}
					else
					{
						flag = true;
						result = false;
					}
				}
			}
			else
			{
				result = false;
			}
			IL_229:
			if (this.currentLine.Words != null)
			{
				this.currentLine.Words.Clear();
			}
			this.currentLine.Words = words;
			this.currentLine.HighlightSpanStack = ((this.currentSpanStack != null && !this.currentSpanStack.IsEmpty) ? this.currentSpanStack : null);
			return result;
		}
		public virtual void MarkTokens(IDocument document, List<LineSegment> inputLines)
		{
			if (this.Rules.Count == 0)
			{
				return;
			}
			Dictionary<LineSegment, bool> dictionary = new Dictionary<LineSegment, bool>();
			bool flag = false;
			int count = document.LineSegmentCollection.Count;
			foreach (LineSegment current in inputLines)
			{
				if (!dictionary.ContainsKey(current))
				{
					int num = current.LineNumber;
					bool flag2 = true;
					if (num != -1)
					{
						while (flag2 && num < count)
						{
							flag2 = this.MarkTokensInLine(document, num, ref flag);
							dictionary[this.currentLine] = true;
							num++;
						}
					}
				}
			}
			if (flag || inputLines.Count > 20)
			{
				document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea));
			}
			else
			{
				foreach (LineSegment current2 in inputLines)
				{
					document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.SingleLine, current2.LineNumber));
				}
			}
			document.CommitUpdate();
			this.currentLine = null;
		}
		private void UpdateSpanStateVariables()
		{
			this.inSpan = (this.currentSpanStack != null && !this.currentSpanStack.IsEmpty);
			this.activeSpan = (this.inSpan ? this.currentSpanStack.Peek() : null);
			this.activeRuleSet = this.GetRuleSet(this.activeSpan);
		}
		private List<TextWord> ParseLine(IDocument document)
		{
			List<TextWord> list = new List<TextWord>();
			HighlightColor highlightColor = null;
			this.currentOffset = 0;
			this.currentLength = 0;
			this.UpdateSpanStateVariables();
			int length = this.currentLine.Length;
			int offset = this.currentLine.Offset;
			int i = 0;
			while (i < length)
			{
				char charAt = document.GetCharAt(offset + i);
				char c = charAt;
				switch (c)
				{
					case '\t':
					{
						this.PushCurWord(document, ref highlightColor, list);
						if (this.activeSpan != null && this.activeSpan.Color.HasBackground)
						{
							list.Add(new TextWord.TabTextWord(this.activeSpan.Color));
						}
						else
						{
							list.Add(TextWord.Tab);
						}
						this.currentOffset++;
						break;
					}
					case '\n':
					case '\r':
					{
						this.PushCurWord(document, ref highlightColor, list);
						this.currentOffset++;
						break;
					}
					case '\v':
					case '\f':
					{
						goto IL_146;
					}
					default:
					{
						if (c != ' ')
						{
							goto IL_146;
						}
						this.PushCurWord(document, ref highlightColor, list);
						if (this.activeSpan != null && this.activeSpan.Color.HasBackground)
						{
							list.Add(new TextWord.SpaceTextWord(this.activeSpan.Color));
						}
						else
						{
							list.Add(TextWord.Space);
						}
						this.currentOffset++;
						break;
					}
				}
				IL_8B2:
				i++;
				continue;
				IL_146:
				char c2 = '\0';
				if (this.activeSpan != null && this.activeSpan.EscapeCharacter != '\0')
				{
					c2 = this.activeSpan.EscapeCharacter;
				}
				else
				{
					if (this.activeRuleSet != null)
					{
						c2 = this.activeRuleSet.EscapeCharacter;
					}
				}
				if (c2 != '\0' && c2 == charAt)
				{
					if (this.activeSpan == null || this.activeSpan.End == null || this.activeSpan.End.Length != 1 || c2 != this.activeSpan.End[0])
					{
						this.currentLength++;
						if (i + 1 < length)
						{
							this.currentLength++;
						}
						this.PushCurWord(document, ref highlightColor, list);
						i++;
						goto IL_8B2;
					}
					if (i + 1 < length && document.GetCharAt(offset + i + 1) == c2)
					{
						this.currentLength += 2;
						this.PushCurWord(document, ref highlightColor, list);
						i++;
						goto IL_8B2;
					}
				}
				if (!this.inSpan && (char.IsDigit(charAt) || (charAt == '.' && i + 1 < length && char.IsDigit(document.GetCharAt(offset + i + 1)))) && this.currentLength == 0)
				{
					bool flag = false;
					bool flag2 = false;
					if (charAt == '0' && i + 1 < length && char.ToUpper(document.GetCharAt(offset + i + 1)) == 'X')
					{
						this.currentLength++;
						i++;
						this.currentLength++;
						flag = true;
						while (i + 1 < length)
						{
							if ("0123456789ABCDEF".IndexOf(char.ToUpper(document.GetCharAt(offset + i + 1))) == -1)
							{
								break;
							}
							i++;
							this.currentLength++;
						}
					}
					else
					{
						this.currentLength++;
						while (i + 1 < length && char.IsDigit(document.GetCharAt(offset + i + 1)))
						{
							i++;
							this.currentLength++;
						}
					}
					if (!flag && i + 1 < length && document.GetCharAt(offset + i + 1) == '.')
					{
						flag2 = true;
						i++;
						this.currentLength++;
						while (i + 1 < length && char.IsDigit(document.GetCharAt(offset + i + 1)))
						{
							i++;
							this.currentLength++;
						}
					}
					if (i + 1 < length && char.ToUpper(document.GetCharAt(offset + i + 1)) == 'E')
					{
						flag2 = true;
						i++;
						this.currentLength++;
						if (i + 1 < length && (document.GetCharAt(offset + i + 1) == '+' || document.GetCharAt(this.currentLine.Offset + i + 1) == '-'))
						{
							i++;
							this.currentLength++;
						}
						while (i + 1 < this.currentLine.Length && char.IsDigit(document.GetCharAt(offset + i + 1)))
						{
							i++;
							this.currentLength++;
						}
					}
					if (i + 1 < this.currentLine.Length)
					{
						char c3 = char.ToUpper(document.GetCharAt(offset + i + 1));
						if (c3 == 'F' || c3 == 'M' || c3 == 'D')
						{
							flag2 = true;
							i++;
							this.currentLength++;
						}
					}
					if (!flag2)
					{
						bool flag3 = false;
						if (i + 1 < length && char.ToUpper(document.GetCharAt(offset + i + 1)) == 'U')
						{
							i++;
							this.currentLength++;
							flag3 = true;
						}
						if (i + 1 < length && char.ToUpper(document.GetCharAt(offset + i + 1)) == 'L')
						{
							i++;
							this.currentLength++;
							if (!flag3 && i + 1 < length && char.ToUpper(document.GetCharAt(offset + i + 1)) == 'U')
							{
								i++;
								this.currentLength++;
							}
						}
					}
					list.Add(new TextWord(document, this.currentLine, this.currentOffset, this.currentLength, this.DigitColor, false));
					this.currentOffset += this.currentLength;
					this.currentLength = 0;
					goto IL_8B2;
				}
				if (this.inSpan && this.activeSpan.End != null && this.activeSpan.End.Length > 0 && DefaultHighlightingStrategy.MatchExpr(this.currentLine, this.activeSpan.End, i, document, this.activeSpan.IgnoreCase))
				{
					this.PushCurWord(document, ref highlightColor, list);
					string regString = DefaultHighlightingStrategy.GetRegString(this.currentLine, this.activeSpan.End, i, document);
					this.currentLength += regString.Length;
					list.Add(new TextWord(document, this.currentLine, this.currentOffset, this.currentLength, this.activeSpan.EndColor, false));
					this.currentOffset += this.currentLength;
					this.currentLength = 0;
					i += regString.Length - 1;
					this.currentSpanStack.Pop();
					this.UpdateSpanStateVariables();
					goto IL_8B2;
				}
				if (this.activeRuleSet != null)
				{
					foreach (Span span in this.activeRuleSet.Spans)
					{
						if (!span.IsBeginSingleWord || this.currentLength == 0)
						{
							if (span.IsBeginStartOfLine.HasValue)
							{
								bool arg_723_0 = span.IsBeginStartOfLine.Value;
                                bool arg_723_1;
								if (this.currentLength == 0)
								{
									arg_723_1 = (list.TrueForAll((TextWord textWord) => textWord.Type != TextWordType.Word) ? true : false);
								}
								else
								{
                                    arg_723_1 = false;
								}
								if (arg_723_0 != arg_723_1)
								{
									continue;
								}
							}
							if (DefaultHighlightingStrategy.MatchExpr(this.currentLine, span.Begin, i, document, this.activeRuleSet.IgnoreCase))
							{
								this.PushCurWord(document, ref highlightColor, list);
								string regString2 = DefaultHighlightingStrategy.GetRegString(this.currentLine, span.Begin, i, document);
								if (!this.OverrideSpan(regString2, document, list, span, ref i))
								{
									this.currentLength += regString2.Length;
									list.Add(new TextWord(document, this.currentLine, this.currentOffset, this.currentLength, span.BeginColor, false));
									this.currentOffset += this.currentLength;
									this.currentLength = 0;
									i += regString2.Length - 1;
									if (this.currentSpanStack == null)
									{
										this.currentSpanStack = new SpanStack();
									}
									this.currentSpanStack.Push(span);
									span.IgnoreCase = this.activeRuleSet.IgnoreCase;
									this.UpdateSpanStateVariables();
								}
								goto IL_8B2;
							}
						}
					}
				}
				if (this.activeRuleSet != null && charAt < 'Ā' && this.activeRuleSet.Delimiters[(int)charAt])
				{
					this.PushCurWord(document, ref highlightColor, list);
					if (this.currentOffset + this.currentLength + 1 < this.currentLine.Length)
					{
						this.currentLength++;
						this.PushCurWord(document, ref highlightColor, list);
						goto IL_8B2;
					}
				}
				this.currentLength++;
				goto IL_8B2;
			}
			this.PushCurWord(document, ref highlightColor, list);
			this.OnParsedLine(document, this.currentLine, list);
			return list;
		}
		protected virtual void OnParsedLine(IDocument document, LineSegment currentLine, List<TextWord> words)
		{
		}
		protected virtual bool OverrideSpan(string spanBegin, IDocument document, List<TextWord> words, Span span, ref int lineOffset)
		{
			return false;
		}
		private void PushCurWord(IDocument document, ref HighlightColor markNext, List<TextWord> words)
		{
			if (this.currentLength > 0)
			{
				if (words.Count > 0 && this.activeRuleSet != null)
				{
					int i = words.Count - 1;
					while (i >= 0)
					{
						if (!words[i].IsWhiteSpace)
						{
							TextWord textWord = words[i];
							if (!textWord.HasDefaultColor)
							{
								break;
							}
							PrevMarker prevMarker = (PrevMarker)this.activeRuleSet.PrevMarkers[document, this.currentLine, this.currentOffset, this.currentLength];
							if (prevMarker != null)
							{
								textWord.SyntaxColor = prevMarker.Color;
								break;
							}
							break;
						}
						else
						{
							i--;
						}
					}
				}
				if (this.inSpan)
				{
					HighlightColor highlightColor = null;
					bool hasDefaultColor = true;
					if (this.activeSpan.Rule == null)
					{
						highlightColor = this.activeSpan.Color;
					}
					else
					{
						highlightColor = this.GetColor(this.activeRuleSet, document, this.currentLine, this.currentOffset, this.currentLength);
						hasDefaultColor = false;
					}
					if (highlightColor == null)
					{
						highlightColor = this.activeSpan.Color;
						if (highlightColor.Color == Color.Transparent)
						{
							highlightColor = this.DefaultTextColor;
						}
						hasDefaultColor = true;
					}
					words.Add(new TextWord(document, this.currentLine, this.currentOffset, this.currentLength, (markNext != null) ? markNext : highlightColor, hasDefaultColor));
				}
				else
				{
					HighlightColor highlightColor2 = (markNext != null) ? markNext : this.GetColor(this.activeRuleSet, document, this.currentLine, this.currentOffset, this.currentLength);
					if (highlightColor2 == null)
					{
						words.Add(new TextWord(document, this.currentLine, this.currentOffset, this.currentLength, this.DefaultTextColor, true));
					}
					else
					{
						words.Add(new TextWord(document, this.currentLine, this.currentOffset, this.currentLength, highlightColor2, false));
					}
				}
				if (this.activeRuleSet != null)
				{
					NextMarker nextMarker = (NextMarker)this.activeRuleSet.NextMarkers[document, this.currentLine, this.currentOffset, this.currentLength];
					if (nextMarker != null)
					{
						if (nextMarker.MarkMarker && words.Count > 0)
						{
							TextWord textWord2 = words[words.Count - 1];
							textWord2.SyntaxColor = nextMarker.Color;
						}
						markNext = nextMarker.Color;
					}
					else
					{
						markNext = null;
					}
				}
				this.currentOffset += this.currentLength;
				this.currentLength = 0;
			}
		}
		private static string GetRegString(LineSegment lineSegment, char[] expr, int index, IDocument document)
		{
			int num = 0;
			StringBuilder stringBuilder = new StringBuilder();
			int i = 0;
			while (i < expr.Length && index + num < lineSegment.Length)
			{
				char c = expr[i];
				if (c == '@')
				{
					i++;
					if (i == expr.Length)
					{
						throw new HighlightingDefinitionInvalidException("Unexpected end of @ sequence, use @@ to look for a single @.");
					}
					char c2 = expr[i];
					if (c2 != '!')
					{
						if (c2 == '@')
						{
							stringBuilder.Append(document.GetCharAt(lineSegment.Offset + index + num));
						}
					}
					else
					{
						StringBuilder stringBuilder2 = new StringBuilder();
						i++;
						while (i < expr.Length)
						{
							if (expr[i] == '@')
							{
								break;
							}
							stringBuilder2.Append(expr[i++]);
						}
					}
				}
				else
				{
					if (expr[i] != document.GetCharAt(lineSegment.Offset + index + num))
					{
						return stringBuilder.ToString();
					}
					stringBuilder.Append(document.GetCharAt(lineSegment.Offset + index + num));
				}
				i++;
				num++;
			}
			return stringBuilder.ToString();
		}
		private static bool MatchExpr(LineSegment lineSegment, char[] expr, int index, IDocument document, bool ignoreCase)
		{
			int i = 0;
			int num = 0;
			while (i < expr.Length)
			{
				char c = expr[i];
				if (c == '@')
				{
					i++;
					if (i == expr.Length)
					{
						throw new HighlightingDefinitionInvalidException("Unexpected end of @ sequence, use @@ to look for a single @.");
					}
					char c2 = expr[i];
					if (c2 <= '-')
					{
						if (c2 != '!')
						{
							if (c2 == '-')
							{
								StringBuilder stringBuilder = new StringBuilder();
								i++;
								while (i < expr.Length && expr[i] != '@')
								{
									stringBuilder.Append(expr[i++]);
								}
								if (index - stringBuilder.Length >= 0)
								{
									int j;
									for (j = 0; j < stringBuilder.Length; j++)
									{
										char c3 = ignoreCase ? char.ToUpperInvariant(document.GetCharAt(lineSegment.Offset + index - stringBuilder.Length + j)) : document.GetCharAt(lineSegment.Offset + index - stringBuilder.Length + j);
										char c4 = ignoreCase ? char.ToUpperInvariant(stringBuilder[j]) : stringBuilder[j];
										if (c3 != c4)
										{
											break;
										}
									}
									if (j >= stringBuilder.Length)
									{
										return false;
									}
								}
							}
						}
						else
						{
							StringBuilder stringBuilder2 = new StringBuilder();
							i++;
							while (i < expr.Length && expr[i] != '@')
							{
								stringBuilder2.Append(expr[i++]);
							}
							if (lineSegment.Offset + index + num + stringBuilder2.Length < document.TextLength)
							{
								int k;
								for (k = 0; k < stringBuilder2.Length; k++)
								{
									char c5 = ignoreCase ? char.ToUpperInvariant(document.GetCharAt(lineSegment.Offset + index + num + k)) : document.GetCharAt(lineSegment.Offset + index + num + k);
									char c6 = ignoreCase ? char.ToUpperInvariant(stringBuilder2[k]) : stringBuilder2[k];
									if (c5 != c6)
									{
										break;
									}
								}
								if (k >= stringBuilder2.Length)
								{
									return false;
								}
							}
						}
					}
					else
					{
						if (c2 != '@')
						{
							if (c2 == 'C' && index + num != lineSegment.Offset && index + num < lineSegment.Offset + lineSegment.Length)
							{
								char charAt = document.GetCharAt(lineSegment.Offset + index + num);
								if (!char.IsWhiteSpace(charAt) && !char.IsPunctuation(charAt))
								{
									return false;
								}
							}
						}
						else
						{
							if (index + num >= lineSegment.Length || '@' != document.GetCharAt(lineSegment.Offset + index + num))
							{
								return false;
							}
						}
					}
				}
				else
				{
					if (index + num >= lineSegment.Length)
					{
						return false;
					}
					char c7 = ignoreCase ? char.ToUpperInvariant(document.GetCharAt(lineSegment.Offset + index + num)) : document.GetCharAt(lineSegment.Offset + index + num);
					char c8 = ignoreCase ? char.ToUpperInvariant(expr[i]) : expr[i];
					if (c7 != c8)
					{
						return false;
					}
				}
				i++;
				num++;
			}
			return true;
		}
	}
}
